package com.dream.myqiyi.service;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.dream.myqiyi.Global;
import com.dream.myqiyi.dao.NoteModel;
import com.dream.myqiyi.dao.NoteResourceModel;
import com.dream.myqiyi.image.util.ImageUtil;

public class NoteResourceService {
	NoteResourceManager noteResMgr = (NoteResourceManager)ApplicationContext.getSingleton().getBean("NoteResourceManager");
	Map<String, NoteResourceModel>			allNoteResource = new HashMap<String, NoteResourceModel>();
	
	public NoteResourceModel saveNoteResourceLocal(String guid, String noteGuid, String fileName) {
		NoteResourceModel nrm = noteResMgr.get(guid);
		if (nrm == null) {
			nrm = new NoteResourceModel();
			nrm.setGuid(guid);
			nrm.setNoteGuid(noteGuid);
			allNoteResource.put(guid, nrm);
		}
		nrm.setAttributeFileName(Global.getFileNameWithEx(fileName));
		nrm.updateSequenceNumber++;
		return nrm;
	}
	
	public NoteResourceModel saveNoteResource(String guid, String noteGuid, String fileName) {
		NoteResourceModel nrm = noteResMgr.get(guid);
		if (nrm == null) {
			nrm = new NoteResourceModel();
			nrm.setGuid(guid);
			nrm.setNoteGuid(noteGuid);
			allNoteResource.put(guid, nrm);
		}
		nrm.setAttributeFileName(Global.getFileNameWithEx(fileName));
		nrm.updateSequenceNumber++;
		
		nrm.setDataBinary(Global.getBytesFromFile(new File(Global.resDir + nrm.getAttributeFileName())));
		noteResMgr.save(nrm);		

		return nrm;
	}
	
	public void saveNoteResource(NoteResourceModel nrm) {
		nrm.setDataBinary(Global.getBytesFromFile(new File(Global.resDir + nrm.getAttributeFileName())));
		noteResMgr.save(nrm);		
	}
	
	public NoteResourceModel getLocalNoteResource(String guid) {
		if (!allNoteResource.containsKey(guid)) {
			return null;
		}
		return allNoteResource.get(guid);
	}
	
	public NoteResourceModel getNoteResource(String guid) {
		NoteResourceModel nrm = noteResMgr.get(guid);
		if (nrm != null) {
			allNoteResource.put(guid, nrm);
		}
		return nrm;
	}
	
	public void  resetDirtyFlag(String guid) {
		NoteResourceModel nrm = noteResMgr.get(guid);
		if (nrm != null) {
			nrm.setDirty(false);
			noteResMgr.update(nrm);
		}
	}
	
	// Set if the resource should be indexed
	public void  setIndexNeeded(String guid, Boolean indexNeeded) {
		NoteResourceModel nrm = noteResMgr.get(guid);
		if (nrm != null) {
			noteResMgr.update(nrm);
		}
	}
	
	// get any unindexed resource
//	public List<String> getNextUnindexed(int limit) {
//		List<String> guids = new ArrayList<String>();
//        NSqlQuery query = new NSqlQuery(db.getResourceConnection());
//        				
//		if (!query.exec("Select guid from NoteResources where indexNeeded = true limit " +limit))
//			logger.log(logger.EXTREME, "NoteResources SQL retrieve has failed on getNextUnindexed(): " +query.lastError());
//
//		// Get a list of the notes
//		String guid;
//		while (query.next()) {
//			guid = new String();
//			guid = query.valueString(0);
//			guids.add(guid);
//		}	
//		return guids;	
//	}
//	// get any unindexed resource
//	public List<String> getUnindexed() {
//		List<String> guids = new ArrayList<String>();
//        NSqlQuery query = new NSqlQuery(db.getResourceConnection());
//        				
//		if (!query.exec("Select guid from NoteResources where indexNeeded = true"))
//			logger.log(logger.EXTREME, "NoteResources SQL retrieve has failed on getUnindexed(): " +query.lastError());
//
//		// Get a list of the notes
//		String guid;
//		while (query.next()) {
//			guid = new String();
//			guid = query.valueString(0);
//			guids.add(guid);
//		}	
//		return guids;	
//	}
//
//	public List<String> getAll() {
//		List<String> guids = new ArrayList<String>();
//		NSqlQuery query = new NSqlQuery(db.getResourceConnection());
//		
//		query.prepare("Select guid from noteresources;");
//		if (!query.exec())
//			logger.log(logger.EXTREME, "Error getting all note resource guids. " +query.lastError());
//		
//		while (query.next()) {
//			guids.add(query.valueString(0));
//		}
//		return guids;
//	}

}
